# -*- coding:utf-8 -*-
'''
Created on 2010-1-28

@author: Administrator
@note: tscad labatory


'''
from scipy.optimize import fsolve  #非线性求解

from scipy.linalg import solve  #线性方程求解

from scipy.interpolate import * #插值

from scipy.integrate import quad #定积分
from scipy.integrate import ode  #常微分

import copy
#from set import * 

def func():
    '''
    定义关系式用!
    对于函数表达式,实验数据等的处理?
    '''
    pass
import time
from math import sin, cos
from scipy.optimize import fsolve
from scipy import pi
from scipy.linalg import solve
import scipy
import numpy

class T:
    def __init__(self):
        self.time = time.clock()
    def o(self):
        return time.clock() - self.time

def f(x):
    x0 = float(x[0])
    x1 = float(x[1])
    x2 = float(x[2])
    return [5 * x1 + 3, 4 * x0 * x0 - 2 * sin(x1 * x2), x1 * x2 - 1.5]
 
def f1(x):
    x1 = float(x[0])
    x2 = float(x[1])
    return [3 * x1 + 5 * x2 + 3, 2 * x1 - 2 * pi * x2 - 5]

#---------begin of numeric calculate ------------
def g1():
    return [('x1',), '5 * x1 + 3']  #if the element is one, should add , after the only element

def g2():
    return [('x0', 'x1', 'x2'), '4 * x0 * x0 - 2 * sin(x1 * x2)']

def g3():
    return [('x1', 'x2'), 'x1 * x2 - 1.5']

def para():
    e1 = g1()[0]
    e2 = g2()[0]
    e3 = g3()[0]
    
    local_var = set(e1) | set(e2) | set(e3)

    return list(local_var)

def g(x):
    '''
    in the order of g1/g2/g3
    '''

    local_var = para()
    n = 0
    for v in local_var:
        locals()[v] = float(x[n])
        n += 1
    
    e = []
    e.append(eval(g1()[1]))
    e.append(eval(g2()[1]))
    e.append(eval(g3()[1]))
    return e

def isovle():
#    result = fsolve(f, [1, 1, 1])
#    print result, f(result), '\n'
    p = para()
    pv = range(len(p))
#    pv = [1 for i in range(len(p))]
    v = fsolve(g, pv)
    d = dict((p[i], v[i]) for i in range(len(p)))
    return d

#---------------end of numeric calculate ----------------

def dsort(d):
    '''
    sort a dict
    '''
    k = d.keys()
    k.sort()
    return dict([(k[i], d[k[i]]) for i in range(len(k))])
    
class A:
    def __init__(self, modules):
        self.modules = modules
        
    def setModules(self, modules):
        self.modules = modules
        
    def __eq__(self, others):
        '''
        modules is a list type
        '''
        n = len(self.modules)
        if n != len(others.modules):
            return False
        for i in range(n):
            if self.modules[i] != others.modules[i]:
                return False
        return True
    
    def __repr__(self):
        return repr(self.modules)
class B:
    def __init__(self, d):
        self.d = d
    
    def __eq__(self, other):
        return True
        
class C:
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def __lt__(self, other):
        return self.a < other.a and self.b < other.b
    def __le__(self, other):
        return self.a <= other.a and self.b <= other.b
        
    def __repr__(self):
        return repr((self.a, self.b))

class D:
    def __init__(self,a,b):
        self.a = a
        self.b = b
        self.id = id(self)
    def setA(self,a):
        self.a = a
    def __eq__(self,other):
        return self.a == other.a

if __name__ == '__main__':
    d1 = D(1,2)
    d2 = copy.copy(d1)
#    d2.setA(4)
    print id(d1),id(d2),d1==d2
    
    
#    r = isovle()
#    print r
#    print g(r.values())
###    t = T()
#    result = fsolve(f, [1, 1, 1])
##    print t.o()
#    print result, f(result), '\n'
##    print f(range(3))
#    print para()
#    p = range(len(para()))
#    r = fsolve(g, p)
#    print r, g(r)
    
#    
#    a = numpy.array([[3, 5], [2, -2 * pi]])
#    b = numpy.array([[3], [-5]])
#    t = T()
#    x = solve(a, b)
#    print t.o(), x
    
#    d = range(5)
#    a = A(d)
#    print a
#    d2 = d[:]
#    d2.append(10)
#    b = A(d2)
#    print a,b
#    print a==b
#    c1 = C(3, range(3))
#    c2 = C(1, range(5))
#    l = [c2, c1]
#    l.sort()
#    print l
#    print c1 == c2
#    print '<', c1 < c2
#    print '>', c1 > c2
#    print '<=', c1 <= c2
#    print '>=', c1 >= c2
#    
